package com.jm.product.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

/**
 * 加载外部配置文件
 * classpath路径：locations={"classpath:application-bean1.xml","classpath:application-bean2.xml"}
 */
@Configuration
//@ImportResource(locations={
//        "classpath:conf/spring-context.xml"
//        "classpath:conf/application-rabbitmq.xml",
//        "classpath:conf/applicationContext-redis.xml",
//        "classpath:conf/applicationContext-shiro.xml",
//        "classpath:conf/applicationContext-mongo.xml"
//})
public class ConfigClass {

	 /**
     * 使用代码注册Servlet（不需要@ServletComponentScan注解）
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean srb = new ServletRegistrationBean();
        srb.setServlet(new org.springframework.web.servlet.DispatcherServlet());// ServletName默认值为首字母小写
        srb.setEnabled(true);
        srb.addInitParameter("contextConfigLocation", "classpath:conf/spring-mvc*.xml");
        srb.setLoadOnStartup(1);
        srb.addUrlMappings("/");
        return srb;
    }
    
    /**
     * 使用代码注册Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean characterEncodingFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new org.springframework.web.filter.CharacterEncodingFilter());
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addInitParameter("encoding","UTF-8");
        filterRegistrationBean.addInitParameter("forceEnCoding","true");
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
    
    /**
     * 使用代码注册Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean hibernateFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new org.springframework.orm.hibernate5.support.OpenSessionInViewFilter());
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addInitParameter("singleSession","true");
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
    
    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }
    
//    @Bean
    public FilterRegistrationBean sessionFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        filterRegistrationBean.setFilter(new com.java.framework.mvc.filter.SessionFilter());
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addInitParameter("cookieDomain","localhost");
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    /**
     * 注册shiro的filter
     * @return
     */
//    @Bean
    public FilterRegistrationBean shiroFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new org.springframework.web.filter.DelegatingFilterProxy());
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addInitParameter("targetFilterLifecycle","true");
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

//    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
//        resolver.setPrefix("");
        resolver.setSuffix(".ftl");
        resolver.setContentType("text/html; charset=UTF-8");
        resolver.setRequestContextAttribute("request");
        return resolver;
    }

}
